<?php
class ProjectsService extends Service{

    
    /**
     * @param int id
     * <pre>
     * {"type":"integer"}
     * </pre>
     * @return void
     */
    public static function delete($id) {
        if (self::_invoke()) return self::_output();
        $project = Doctrine::getTable('Project')->find($id);
        $project->delete();
    }

    /**
     * @param int id
     * <pre>
     * {"type":"integer"}
     * </pre>
     * @return string
     */
    public static function export($id) {
        if (self::_invoke()) return self::_output();
        $project = Doctrine::getTable('Project')->find($id);
        $ar = array();
        //$ar['project'] = $project->export();
        //$ar['tasks']   = $project->DevTask->toExportArray();
        $tests = Doctrine::getTable('Test')->findAll();
        $arTests = array();
        foreach($tests as $test) {
            $arTests[] = $test->export();
        }
        $ar['tests']   = $arTests;
        $fileContents = json_encode($ar);
        $name = $project['id'].'_';
        $name .= GraduaString::removeSpacesAndCapitalizeFirstLetters($project['name']);
        $name .= '_TESTS_'.date('Y_m_d_h_i');
        $name .= '.phpTasks';
        $filePath = sfConfig::get('sf_data_dir').DIRECTORY_SEPARATOR.'exported_files'.DIRECTORY_SEPARATOR.$name;
        file_put_contents(
            $filePath,
            $fileContents
        );
        return $filePath;

        //header
        
    }
    
    /**
     * @param object $data
     * <pre>
     * {
     *   "type":"object",
     *   "properties": {
     *     "id":{"type":"integer"},
     *     "filePath":{"type":"string"}
     *   }
     * }
     * </pre>
     * @return void
     */
    public static function import($data) {
        if (self::_invoke()) return self::_output();
        $st = file_get_contents($data->filePath);
        $imp = json_decode($st);
        $project = Doctrine::getTable('Project')->find($data->id);
        $conn = Doctrine_Manager::connection();
        $conn->beginTransaction();
        try {
            //$project->deleteTestsAndTasks();
            foreach($imp->tests as $testData) {
                TestTable::importTest($testData,$project['id']);
            }
            /*
            $root = Doctrine::getTable('DevTask')->getTree()->findRoot();
            $imp['tasks']['project_id'] = $project['id'];
            $root->importTask($imp['tasks'], $project['id']);
            */
            $conn->commit();
        }
        catch(Exception $e) {
            $conn->rollback();
            throw $e;
        }
    }

    

    /**
     * @param object $data
     * <pre>
     * {
     *   "type":"object",
     *   "properties": {
     *     "id":{"type":"string"},
     *     "name":{"type":"string","minLength":5},
     *     "tests_dir":{"type":"string","minLength":2},
     *     "tests_result_dir":{"type":"string","minLength":2},
     *     "run_all_tests_dir_order":{"type":"string","minLength":2},
     *     "use_tests":{"type":"boolean"}
     *   }
     * }
     * </pre>
     * @return void
     */
    public static function edit($data) {
        if (self::_invoke()) return self::_output();
        $project = Doctrine::getTable('Project')->find($data->id);
        $project->name = $data->name;
        $project->tests_dir = $data->tests_dir;
        $project->tests_result_dir = $data->tests_result_dir;
        $project->run_all_tests_dir_order = $data->run_all_tests_dir_order;
        $project->use_tests = $data->use_tests;
        $project->save();
    }

    /**
     * @param object $data
     * <pre>
     * {
     *   "type":"object",
     *   "properties": {
     *     "name":{"type":"string","minLength":5},
     *     "tests_dir":{"type":"string","minLength":2},
     *     "tests_result_dir":{"type":"string","minLength":2},
     *     "run_all_tests_dir_order":{"type":"string","minLength":2},
     *     "use_tests":{"type":"boolean"}
     *   }
     * }
     * </pre>
     * @return int
     */
    public static function insert($data) {
        if (self::_invoke()) return self::_output();
        $conn = Doctrine_Manager::connection();
        $conn->beginTransaction();

        $project = new Project;
        $project->name = $data->name;
        $project->tests_dir = $data->tests_dir;
        $project->tests_result_dir = $data->tests_result_dir;
        $project->run_all_tests_dir_order = $data->run_all_tests_dir_order;
        $project->use_tests = $data->use_tests;
        $project->save();

        DevTaskTable::createProjectRootNode($project->name, $project->id);

        $conn->commit();
        return $project->id;
    }
    
    /**
     * @param int id
     * @return object
     * {
     *   "type":"object",
     *   "properties": {
     *     "id":{"type":"string"},
     *     "name":{"type":"string","minLength":5},
     *     "tests_dir":{"type":"string","minLength":2},
     *     "tests_result_dir":{"type":"string","minLength":2},
     *     "run_all_tests_dir_order":{"type":"string","minLength":2},
     *     "use_tests":{"type":"boolean"}
     *   }
     * }
     */
    public static function get($id) {
        $project = Doctrine::getTable('Project')->find($id);
        $project = $project->toArray();
        $project['tests_dir_exists'] = file_exists($project['tests_dir']);
        $project['tests_result_dir_exists'] = file_exists($project['tests_result_dir']);
        return $project;
    }

    /**
     * @param boolean $verifyDirs
     * @return object
     * <pre>
     * {
     *   "type":"object",
     *   "properties": {
     *     "id":{"type":"string"},
     *     "name":{"type":"string","minLength":5},
     *     "tests_dir":{"type":"string","minLength":2},
     *     "tests_result_dir":{"type":"string","minLength":2},
     *     "run_all_tests_dir_order":{"type":"string","minLength":2},
     *     "use_tests":{"type":"boolean"}
     *   }
     * }
     * </pre>
     */
    public static function getAll($verifyDirs = true) {
        $projects = Doctrine::getTable('Project')->findAll(Doctrine::HYDRATE_ARRAY);
        if($verifyDirs) {
            foreach($projects as &$project) {
                $project['tests_dir_exists'] = file_exists($project['tests_dir']);
                $project['tests_result_dir_exists'] = file_exists($project['tests_result_dir']);
            }
        }
        return $projects;
    }


}